﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_FirePillars : TargetAreaSpell
    {
        private int DamageLow = 250;
        private int DamageHigh = 350;

        private int ResilienceHealLow = 300;
        private int ResilienceHealHigh = 400;

        private int MightDamageLow = 300;
        private int MightDamageHigh = 400;

        public void InstantCast(CreatureBase castingCreatureBase, CreatureBase targetCreatureBase, SpellIcon sIcon, int CastLevel)
        {
            if (base.InitializeInstantCast(castingCreatureBase, targetCreatureBase, sIcon, CastLevel, false) == false)
                return;

            MainCast(castingCreatureBase);
        }

        private void MainCast(CreatureBase castingCreatureBase)
        {
            List<Character> CharactersInUpdateRange = new List<Character>();
            foreach (MapItem mapitem in TargetCreatureBase.currentMapItem.mapItemsWithin4)
            {
                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharactersInUpdateRange.Add((Character)creaturebase);
                }
            }

            TargetCreatureBase.currentMapItem.SpellLayer0.Add(this);
            MapItems.Add(TargetCreatureBase.currentMapItem);

            //Build a list of creatures first
            List<CreatureBase> creaturesToEffect = new List<CreatureBase>();
            foreach (CreatureBase creaturebase in TargetCreatureBase.currentMapItem.CreaturesBases)
            {
                //Resilience heals the caster, (This is from the Resilience death effect)
                if (spellIcon.Skill == "Resilience")
                {
                    if (creaturebase.HP > 0 && creaturebase.Faction == castingCreatureBase.Faction)
                        creaturesToEffect.Add(creaturebase);
                }
                else
                {
                    if (creaturebase.HP > 0 && creaturebase.Faction == TargetCreatureBase.Faction)
                        creaturesToEffect.Add(creaturebase);
                }
            }

            //Damage them later, damaging them can cause creatures to be removed from the tile which will break the above iteration
            foreach (CreatureBase creaturebase in creaturesToEffect)
            {
                if (creaturebase.HP > 0)
                {
                    //Get the creature we would give a SD flag to.
                    Character characterToApplyFlag = null;

                    //If the creature we're casting at is a pet, get its master
                    Pet_Controller pc = creaturebase.petController;
                    //Has pet controller, check if master is character
                    if (pc != null && pc.MasterCreature is Character)
                        characterToApplyFlag = (Character)pc.MasterCreature;
                    //No pet controller, check if target is character
                    else if (pc == null && creaturebase is Character)
                        characterToApplyFlag = (Character)creaturebase;

                    //If we're attacking a player, and we dont have a flag on them, give them a flag on us.
                    if (characterToApplyFlag != null &&
                        castingCreatureBase is Character &&
                        characterToApplyFlag != castingCreatureBase &&
                        castingCreatureBase.hateManager.GetCreatureHateInfo(characterToApplyFlag) == null &&
                        spellIcon.Offensive == true)
                    {
                        Character charToAttack = (Character)characterToApplyFlag;
                        if (charToAttack.friendsManager.hasNoSDFlag(CastingCharacterID) == false)
                        {
                            if (creaturebase.hateManager.GetCreatureHateInfo(castingCreatureBase) == null)
                            {
                                creaturebase.SendMessage("You have been ATTACKED by " + castingCreatureBase.Name + "!", "damaged");
                                creaturebase.PlaySound("Ding", creaturebase.MapX, creaturebase.MapY);

                                creaturebase.hateManager.UpdateList(castingCreatureBase, 0);


                                charToAttack.AddMapItem(castingCreatureBase.currentMapItem);
                            }
                            else
                                creaturebase.hateManager.UpdateList(castingCreatureBase, 0);
                        }
                    }


                    int Damage = 0;
                    int ActualDamage = 0;
                    int absorbedDamage = 0;
                    if (spellIcon.Skill == "Resilience")
                    {
                        Damage = Managers.GameHandler.random.Next(ResilienceHealLow, ResilienceHealHigh + 1);
                        Damage = SkillManager.AdjustForCombat(CastingCreatureLevel, Damage, CastingCreatureWis);
                        ActualDamage = Damage;
                        creaturebase.HP = creaturebase.HP + Damage;
                    }
                    else
                    {
                        if (spellIcon.Skill == "Might")
                            Damage = Managers.GameHandler.random.Next(MightDamageLow, MightDamageHigh + 1);
                        else
                            Damage = Managers.GameHandler.random.Next(DamageLow, DamageHigh + 1);

                        Damage = SkillManager.AdjustForCombat(CastingCreatureLevel, Damage, CastingCreatureInt);
                        ActualDamage = creaturebase.CalculateGetSpellDamagePerResists(Damage, spellIcon.Skill);
                        creaturebase.InflictSpellDamage(ActualDamage, CastingCharacterID, CastingCreatureGUID, spellIcon.Skill, out absorbedDamage);
                    }

                    creaturebase.Update_Character();

                    ActualDamage = ActualDamage - absorbedDamage;

                    if (castingCreatureBase is Character)
                    {
                        if (spellIcon.Skill == "Resilience")
                            castingCreatureBase.SendMessage("You heal " + creaturebase.Name + " (" + ActualDamage.ToString() + ")", "spelldefense");
                        else
                            castingCreatureBase.SendMessage("You burn " + creaturebase.Name + "! (" + ActualDamage.ToString() + ")", "spelloffense");

                        Character character = (Character)castingCreatureBase;
                    }
                    if (creaturebase != castingCreatureBase)
                    {
                        if (spellIcon.Skill == "Resilience")
                            creaturebase.SendMessage(castingCreatureBase.Name + " heals you (" + ActualDamage.ToString() + ")", "spelldefense");
                        else
                            creaturebase.SendMessage(castingCreatureBase.Name + " burns you! (" + ActualDamage.ToString() + ")", "spelloffense");
                    }
                }
            }
            for (int x = 0; x < TargetCreatureBase.currentMapItem.CreaturesBases.Count; x++)
            {
                CreatureBase creaturebase = (CreatureBase)TargetCreatureBase.currentMapItem.CreaturesBases[x];
                if (creaturebase.HP < 1)
                {
                    if (!(creaturebase is Character))
                    {
                        TargetCreatureBase.currentMapItem.CreaturesBases.RemoveAt(x);
                        x--;
                    }
                }
            }
            foreach (Character character in CharactersInUpdateRange)
            {
                //character.PlaySound("Lightening", targetsMapItem.MapX, targetsMapItem.MapY);
                character.AddMapItem(TargetCreatureBase.currentMapItem);
            }
        }

        public void Cast(CreatureBase castingCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(castingCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            MainCast(castingCreatureBase);
        }
    }
}
